<template>
  <el-dialog
    :title="!dataForm.gsmGroupRecordId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="活动id" prop="gsmGroupId">
      <el-input v-model="dataForm.gsmGroupId" placeholder="活动id"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="usmId">
      <el-input v-model="dataForm.usmId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="psmName">
      <el-input v-model="dataForm.psmName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品展示" prop="psmImg">
      <el-input v-model="dataForm.psmImg" placeholder="商品展示"></el-input>
    </el-form-item>
    <el-form-item label="商品原价" prop="psmPrice">
      <el-input v-model="dataForm.psmPrice" placeholder="商品原价"></el-input>
    </el-form-item>
    <el-form-item label="拼团价格" prop="gsmGroupPrice">
      <el-input v-model="dataForm.gsmGroupPrice" placeholder="拼团价格"></el-input>
    </el-form-item>
    <el-form-item label="当前参与人数" prop="gsmGroupJoinCount">
      <el-input v-model="dataForm.gsmGroupJoinCount" placeholder="当前参与人数"></el-input>
    </el-form-item>
    <el-form-item label="成团人数" prop="gsmGroupCount">
      <el-input v-model="dataForm.gsmGroupCount" placeholder="成团人数"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="gsmGroupRecordStatus">
      <el-input v-model="dataForm.gsmGroupRecordStatus" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="成团时效" prop="gsmGroupDuretion">
      <el-input v-model="dataForm.gsmGroupDuretion" placeholder="成团时效"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          gsmGroupRecordId: 0,
          gsmGroupId: '',
          usmId: '',
          psmName: '',
          psmImg: '',
          psmPrice: '',
          gsmGroupPrice: '',
          gsmGroupJoinCount: '',
          gsmGroupCount: '',
          gsmGroupRecordStatus: '',
          gsmGroupDuretion: ''
        },
        dataRule: {
          gsmGroupId: [
            { required: true, message: '活动id不能为空', trigger: 'blur' }
          ],
          usmId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          psmName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          psmImg: [
            { required: true, message: '商品展示不能为空', trigger: 'blur' }
          ],
          psmPrice: [
            { required: true, message: '商品原价不能为空', trigger: 'blur' }
          ],
          gsmGroupPrice: [
            { required: true, message: '拼团价格不能为空', trigger: 'blur' }
          ],
          gsmGroupJoinCount: [
            { required: true, message: '当前参与人数不能为空', trigger: 'blur' }
          ],
          gsmGroupCount: [
            { required: true, message: '成团人数不能为空', trigger: 'blur' }
          ],
          gsmGroupRecordStatus: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          gsmGroupDuretion: [
            { required: true, message: '成团时效不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.gsmGroupRecordId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.gsmGroupRecordId) {
            this.$http({
              url: this.$http.adornUrl(`/group/grouprecord/info/${this.dataForm.gsmGroupRecordId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.gsmGroupId = data.groupRecord.gsmGroupId
                this.dataForm.usmId = data.groupRecord.usmId
                this.dataForm.psmName = data.groupRecord.psmName
                this.dataForm.psmImg = data.groupRecord.psmImg
                this.dataForm.psmPrice = data.groupRecord.psmPrice
                this.dataForm.gsmGroupPrice = data.groupRecord.gsmGroupPrice
                this.dataForm.gsmGroupJoinCount = data.groupRecord.gsmGroupJoinCount
                this.dataForm.gsmGroupCount = data.groupRecord.gsmGroupCount
                this.dataForm.gsmGroupRecordStatus = data.groupRecord.gsmGroupRecordStatus
                this.dataForm.gsmGroupDuretion = data.groupRecord.gsmGroupDuretion
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/group/grouprecord/${!this.dataForm.gsmGroupRecordId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'gsmGroupRecordId': this.dataForm.gsmGroupRecordId || undefined,
                'gsmGroupId': this.dataForm.gsmGroupId,
                'usmId': this.dataForm.usmId,
                'psmName': this.dataForm.psmName,
                'psmImg': this.dataForm.psmImg,
                'psmPrice': this.dataForm.psmPrice,
                'gsmGroupPrice': this.dataForm.gsmGroupPrice,
                'gsmGroupJoinCount': this.dataForm.gsmGroupJoinCount,
                'gsmGroupCount': this.dataForm.gsmGroupCount,
                'gsmGroupRecordStatus': this.dataForm.gsmGroupRecordStatus,
                'gsmGroupDuretion': this.dataForm.gsmGroupDuretion
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
